
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:fultter/config/language_cinfig.dart';

void main()=> runApp(LanuageMainApp());

/// 创建一个全局key 用来全局内调用语言切换
GlobalKey<_ChangeLocalizationState> _globalKey = new GlobalKey();
class LanuageMainApp extends StatelessWidget {


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: new Builder(builder: (context){
        return new ChangeLocalization(LanuageHomePage(),key:_globalKey);
      }),
      localizationsDelegates: [
        /// 初始化默认的 Material 组件本地化
        GlobalMaterialLocalizations.delegate,
        /// 初始化默认的 通用Widget 组件本地化
        GlobalWidgetsLocalizations.delegate,
        /// 自定义的语言配制文件代理 初始化
        MyLocationsLanguaeDelegates.delegates,
      ],
      /// 当前区域 如果为null则使用系统统一用于语言切换
      /// 传入两个参数 语言代码 国家代码
      ///  这里配置为中国
      locale: Locale('zh','CN'),
      /// 定义当前应用程序所支持的语言环境
      supportedLocales: [
        const Locale('en','US'),// 英文
        const Locale('he','IL'),// 西班牙
        const Locale('zh','CN'), // 中文
      ],

    );
  }
}

// 创建一个 在应用内切换 语言环境的类
class ChangeLocalization extends StatefulWidget {
  final Widget child;

  ChangeLocalization(this.child,{Key key}):super(key:key);

  @override
  _ChangeLocalizationState createState() => _ChangeLocalizationState();
}

class _ChangeLocalizationState extends State<ChangeLocalization> {
  // 当前的语言环境 中文
  Locale _locale = const Locale('zh','CH');
  changeLocale(Locale locale){
    setState(() {
      _locale = locale;
    });
  }
  @override
  Widget build(BuildContext context) {
    /// 参数一 使用到的语言环境
    /// 参数 二 子widget
    return new Localizations.override(
        context: context,
        locale: _locale,
        child: widget.child,
    );
  }
}



// 创建默认的首页面
class LanuageHomePage extends StatefulWidget {


  @override
  State<StatefulWidget> createState(){
    return new LanuageHomePageState();
  }
}

class LanuageHomePageState extends State<LanuageHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Flutter 国际化配置"),
      ),
      body:Column(
        children: <Widget>[
          FlatButton(
              onPressed: (){
                switchLanguage(0);
              },
              child: Text(MyLocationsLanguages.of(context).button1)
          ),
          FlatButton(
              onPressed: (){
                switchLanguage(1);
              },
              child: Text(MyLocationsLanguages.of(context).button2)
          ),
          FlatButton(
              onPressed: (){
                switchLanguage(2);
              },
              child: Text(MyLocationsLanguages.of(context).button3)
          ),
        ],
      )
    );
  }
  /// 用来切换语言环境
  void switchLanguage(num index){
    switch(index){
      case 0:
        /// 中文
        _globalKey.currentState.changeLocale(Locale('zh','CN'));
        break;
      case 1:
      /// 中文
        _globalKey.currentState.changeLocale(Locale('en','US'));
        break;
      case 2:
      /// 中文
        _globalKey.currentState.changeLocale(Locale('he','IL'));
        break;
    }
    
  }
}

